package com.example.swim.controller;


import com.example.swim.Pojo.Activity;
import com.example.swim.Pojo.Card;
import com.example.swim.Pojo.ClassRoom;
import com.example.swim.Pojo.Coach;
import com.example.swim.Service.AcService;
import com.example.swim.Service.CardService;
import com.example.swim.Service.ClassRoomService;
import com.example.swim.Service.CoachService;
import com.example.swim.Utils.R;
import jakarta.validation.Valid;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author LiYa
 * @create 2022-07-13 12:06
 */
@RestController
@RequestMapping("/swim")
public class IndexController {
//    近期活动信息
    @Autowired
AcService acService;
//    教练信息
    @Autowired
CoachService coachService;
//    开班信息
    @Autowired
ClassRoomService classRoomService;
    @Autowired
    CardService cardService;
    //    首页
    @GetMapping("/index")
    public R toIndex(Model model)
    {
        List<Activity> activities= acService.findAll();
        List<Coach> coaches=coachService.findAll();
        List<ClassRoom> classes=classRoomService.findAll();
        List<Card> cards=cardService.find();
        model.addAttribute("classes", classes);
        model.addAttribute("coaches",coaches);
        model.addAttribute("activities", activities);
        model.addAttribute("cards", cards);
        return new R(true,model);
    }
//     近期活动列表，点击跳转至活动详情页面，会员和学员可参加活动；
    @GetMapping("/ac")
    public R toAc(@RequestParam Integer id, Model model)
    {
        Activity activity=acService.findAc(id);
        return new R(true,activity);
    }
//    教练图片列表，点击跳转至教练详细介绍页面；
    @GetMapping("/co")
    public R toCoach(@RequestParam int id)
    {
        Coach coach=coachService.findCoach(id);
        return new R(true, coach);
    }
//    游泳班开班信息，点击跳转至开班详细介绍页面
    @GetMapping("/Class")
    public R toClass(@RequestParam Integer cid,@RequestParam String name) throws JSONException {
        ClassRoom classRoom=classRoomService.findByName(cid);
//      通过多表查询找到教练名字传递过去
        String coach_name=classRoomService.findCoach(name);
        HashMap<String,Object> map= new HashMap<>();
        map.put("classRoom",classRoom);
        map.put("coach_name",coach_name);
        return new R(true,map);
    }

//    游泳班查询信息
    @GetMapping("/findCR")
    public R findCR(@RequestParam String type,@RequestParam String kind){
        return new R(true,classRoomService.find(type,kind));
    }

}
